package mao.exaimnation.huawei.huawei;



import java.util.Scanner;
//可以到达与否，深度优先搜索。
public class Main2 {
    private static int m,n,step;
    private static boolean canReach;
    private static int[][] dir;
    private static boolean[][] marked;
    static boolean[][] floor;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            step = scanner.nextInt();
            m = scanner.nextInt();
            n = scanner.nextInt();
            canReach = false;
            floor = new boolean[m][n];
            dir = new int[][]{{-step,0},{+step,0},{0,-step},{0,+step}};

            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    floor[i][j] = scanner.nextInt()==1?true:false;
                }
            }
            marked = new boolean[m][n];
            dfs(0,0);
            if (canReach){
                System.out.println(1);
            }else {
                System.out.println(0);
            }
        }

    }
    public static void dfs(int i,int j){
        if (canReach) return;
        if ( !floor[i][j] || marked[i][j]) return;
        marked[i][j] = true;
        if (i==m-1 && j==n-1){
            canReach = true;
            return;
        }
        for (int k = 0; k < 4; k++) {
            int row = i+dir[k][0];
            int col = j+dir[k][1];
            if (checkBound(row,col)){
                dfs(row,col);
            }
        }
        marked[i][j] = false;
    }
    private static boolean checkBound(int i,int j){
        return i>=0 && i<m && j>=0 && j<n;
    }
}
